<script setup>
import { reactive, ref ,onMounted} from 'vue'
import { checkMobile, checkPassword } from '@/utils/validate'
import Upload from '@/components/upload/index.vue'
import {addUser} from '@/api/user'
import {getRoleList} from '@/api/role'

// const emit=defineEmits(['flush'])
// 表单数据
const formDataR = reactive({
  userName: '',
  password: ''
})

// 表单验证
const formRules = reactive({
  // username: [
  //   { required: true, message: '请输入用户名', trigger: 'blur' },
  //   { min: 2, max: 6, message: '请输入2~6个字符', trigger: 'blur' }
  // ],
  // password: [
  //   { validator: checkPassword, message: '请输入6~12个字符', trigger: 'blur' }
  //   // { required: true, message: '请输入密码', trigger: 'blur' },
  //   // { min: 6, max: 12, message: '请输入6~12个字符', trigger: 'blur' }
  // ],
  // mobile: [
  //   { required: true, message: '请输入手机号', trigger: 'blur' },
  //   { validator: checkMobile, trigger: 'blur' }
  // ]
})
// 提交验证
const formRef = ref()
const onSubmit = () => {
  console.log(formDataR);
  formRef.value.validate(async(isSuccess) => {
    if (isSuccess) {
      //请求接口 //新增
    let r= await addUser(formDataR)
      if(r.code==200){
        ElMessage({type: 'success', message: '注册成功' })
        state.value=false
        //调用父组件的函数，实现列表刷新
        onReset
      }else{
        ElMessage({type: 'success', message: '操作失败' })
      }
    }
  })
}
const onReset = () => {
  formRef.value.resetFields()
//   emit('flush')
  formData.value = {}
}

// 对话框状态
const state = ref(false)

// 暴露
defineExpose({
  state,
  formDataR
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="注册用户"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="default"
      label-width="80"
      :model="formDataR"
      :rules="formRules"
      ref="formRef"
    >
      <el-row>
        <el-col :span="16">
          <el-form-item label="用户名" prop="userName">
            <el-input placeholder="请输入用户名" v-model="formDataR.userName" />
          </el-form-item>
        </el-col>
       
      </el-row>
      <el-row>
      <el-col :span="16">
          <el-form-item label="密码" prop="password">
            <el-input placeholder="请输入密码" v-model="formDataR.password" />
          </el-form-item>
        </el-col>
    </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="手机号" prop="phone">
            <el-input placeholder="请输入手机号" v-model="formDataR.mobile" />
          </el-form-item>
        </el-col>
        
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="mobile">
            <el-input placeholder="请输入邮箱" v-model="formDataR.email" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-select placeholder="请选择性别" style="width: 180px">
              <el-option
                v-for="item in [
                  {
                    value: 1,
                    label: '男'
                  },
                  {
                    value: 0,
                    label: '女'
                  }
                ]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
       
      </el-row>
      <!-- <el-form-item label="头像"> <Upload /> </el-form-item> -->

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>
